/*
 * @Author: cl
 * @Description: 上传文件装饰器
 * @Date: 2023-05-05 16:04:20
 * @LastEditTime: 2023-05-05 17:02:57
 * @FilePath: \jsnode\nestjs\naota\src\upload\upload.detorator.ts
 * @LastEditors: cl
 */
import { applyDecorators, MethodNotAllowedException, UseInterceptors, } from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface'

export function FileFilter(fileName:string = 'file',option?:MulterOptions) {
    return applyDecorators(UseInterceptors(FileInterceptor(fileName,option)))
//   return applyDecorators(UseInterceptors(FileInterceptor(fileName,{
//     limits:{fileSize:1024*1024},
//     fileFilter:(req:any,file:Express.Multer.File,callback:(error:Error|null,acceptFile:boolean)=>void)=>{
//       if(file.mimetype.includes('image')){
//         callback(null,true)
//       }else{
//         callback(new MethodNotAllowedException('文件类型不正确'),false)
//       }
//     }
//   })))
}

export function fileType(type:string[]){
    return (req:any,file:Express.Multer.File,callback:(error:Error|null,acceptFile:boolean)=>void)=>{
        const check = type.some(t=>file.mimetype.includes(t))
      if (check) {
        callback(null, true)
      } else {
        callback(new MethodNotAllowedException('文件类型不正确'), false)
      }
    }
}

